package cn.kgc.controller;

import cn.kgc.pojo.*;
import cn.kgc.service.*;
import cn.kgc.pojo.Client;
import cn.kgc.pojo.Detail;
import cn.kgc.pojo.Order;
import cn.kgc.pojo.Room;
import cn.kgc.service.ClientService;
import cn.kgc.service.DetailService;
import cn.kgc.service.OrderService;
import cn.kgc.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 肖天锴
 * @version 1.0
 * @date 2022/10/7 - 17:19
 */
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ClientService clientService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private DetailService detailService;
    @Autowired
    private RevenueService revenueService;
    @Autowired
    DisburseService disburseService;

    Revenue revenue = new Revenue();
    Disburse disburse = new Disburse();



    @RequestMapping("/show")
    public String show()
    {
        return "showDetail";
    }

    ///渲染订单数据
    @RequestMapping("/showDetail")
    @ResponseBody
    public Map<String,Object> showDetail(String page,String limit,String clientName,String orderState){

        HashMap<String,Object> map=new HashMap<String, Object>();
        int limits = Integer.parseInt(limit);
        int crrPage=1;
        if(page!=null && !"".equals(page)){
            crrPage = Integer.parseInt(page);
        }
        crrPage=(crrPage-1)*limits;

        List<Order> ordersList = orderService.showDetail(crrPage,limits,clientName,orderState);
        int totalPage = orderService.getTotalPage(clientName,orderState);
        map.put("code",0);
        map.put("msg","");
        map.put("count",totalPage);
        map.put("data",ordersList);
        return map;
    }

    //新增订单
    @RequestMapping("/addOrder")
    @ResponseBody
    public String addOrder(Order orders, String clientName, String phone, String idCard, Integer roomNum, java.sql.Date startTime, java.sql.Date deadTime){

        orders.setOrderDate(new Date());
        Detail detail=new Detail();

        //随机数
        orders.setOrderNum(UUID.randomUUID().toString());
        //添加客户信息
        clientService.ClientAdd1(phone,idCard,clientName);
        //通过客户信息获得客户id
        Client client1 = clientService.getByName(clientName);
        int clientId = client1.getClientId();

        orders.setClientId(clientId);
        Room roomByNum = roomService.findRoomByNum(roomNum);
        int roomId = roomByNum.getRoomId();
        orders.setRoomId(roomId);

        orderService.addOrder(orders);
        Order byClientId = orderService.findByClientId(clientId);
        int orderId = byClientId.getOrderId();
        detail.setDeadTime(deadTime);
        detail.setStartTime(startTime);
        detail.setOrderId(orderId);
        detail.setRoomId(roomId);
        detailService.addDetail(detail);
        return "showDetail";
    }

    //查询订单
    @RequestMapping("/find/{orderId}")
    public String find(Model model, @PathVariable("orderId") Integer orderId){
        Order order = orderService.find(orderId);
        Detail detail = order.getDetail();
        Room room = order.getRoom();

        model.addAttribute("order",order);
        model.addAttribute("room",room);
        model.addAttribute("detail",detail);
        return "findAll";
    }
    //根据id查询订单
    @RequestMapping("/findById/{orderId}")
    public String findById(Model model, @PathVariable("orderId") Integer orderId){

        Order order = orderService.findById(orderId);
        model.addAttribute("order",order);

        return "update";
    }
    //修改订单
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> update(Order order){
        HashMap<String,Object> map=new HashMap<>();
        int orderUpdate= orderService.update(order);
        System.out.println(order);
        map.put("code",0);
        map.put("msg","");
        map.put("data",orderUpdate);
        return map;
    }

    //根据id修改订单
    @RequestMapping(value = "/updateById",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateById(Order order){
        HashMap<String,Object> map=new HashMap<>();
        int updateById= orderService.updateById(order);

        map.put("code",0);
        map.put("msg","");
        map.put("data",updateById);
        return map;
    }
    //根据订单id查询订单（支付）
    @RequestMapping("/findByOrderId")
    @ResponseBody
    public String findByOrderId(Integer orderId){
        Order order = orderService.findByOrderId(orderId);
        System.out.println("order="+order);
        System.out.println("order.getDetail()="+order.getDetail());

        revenue.setOrderId(order.getOrderId());
        revenue.setDetailId(order.getDetail().getDetailId());
        revenue.setMethod("微信");
        revenue.setTrasfAccount("621008799523786");
        revenue.setRoomId(order.getRoomId());
        System.out.println("revenue="+revenue);


        String orderState = order.getOrderState();
        String a;
        if (!orderState.equals("已支付")){
            a="1";
        }else {
            a="0";
        }
        System.out.println("=============="+a);
        return a;
    }



    //修改订单状态（支付）
    @RequestMapping("/changeState")
    @ResponseBody
    public int changeState(Integer orderId){
        int i = orderService.changeState(orderId);
        System.out.println("i="+i);
        if(i>0) {
            System.out.println("进来了");
            int a= revenueService.addRevenue(revenue);
            System.out.println("a="+a);
        }
        return i;
    }





    //根据订单id查询订单（退单）
    @RequestMapping("/findByOrderId1")
    @ResponseBody
    public String findByOrderId1(Integer orderId){
        Order order = orderService.findByOrderId1(orderId);
        System.out.println("order="+order);

        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        String format = sdf.format(date);
        System.out.println("现在时间：" + format); // 输出已经格式化的现在时间（24小时制）

        disburse.setRoomId(order.getRoomId());
        disburse.setClientId(order.getClientId());
        disburse.setOrderId(order.getOrderId());
        disburse.setPaidTime(format);

        String orderState = order.getOrderState();

        String a;
        if (orderState.equals("已支付")){
            a="1";
        }else {
            a="0";
        }
        System.out.println("a="+a);
        return a;
    }




    //修改订单状态（退单）
    @RequestMapping("/changeState1")
    @ResponseBody
    public int changeState1(Integer orderId){
        int i = orderService.changeState1(orderId);
        System.out.println("i=="+i);

        if(i>0){
            int aaa= disburseService.addDisburse(disburse);
            System.out.println("aaa=="+aaa);
        }
        return i;
    }


}
